/**
 * Created by ZZP on 2020/3/3.
 */
(function(window) {
    const postMaker = window.postMaker || (window.postMaker=init);

    function init(poster_id,config,w,fn){
        if(typeof w == "function"){
            fn = w
            w = false
        }
        if(typeof config == "function"){
            fn = config
            config = []
        }

        if(!w) w = 500
	var date = new Date();
	var y= date.getFullYear()
	var M=date.getMonth()
	var d =date.getDate();
	var h=date.getHours()
	var m=date.getMinutes()
	var s=date.getSeconds()
	M=M+1
        ajaxGet("https://hb.4coder.org.cn/poster_json/poster_"+poster_id+".json"+'?time='+y+""+M+""+d+""+h+""+m+s,function(res){
            var _info = JSON.parse(res)

            for(var i in config){
                var item = _info.config[i]||false
                if(!item) continue
                if(item.type == 1){
                    item.text = config[i]
                }
                else if(item.type == 2){
                    item.url = config[i]
                }
            }
            
            fn&&buildPreview(_info,w,fn)
        })
    }

    function ajaxGet(url,fn){
        var httpRequest = new XMLHttpRequest();//绗竴姝ワ細寤虹珛鎵€闇€鐨勫璞�
        httpRequest.open('GET', url, true);//绗簩姝ワ細鎵撳紑杩炴帴  灏嗚姹傚弬鏁板啓鍦╱rl涓�  ps:"./Ptest.php?name=test&nameone=testone"
        httpRequest.send();//绗笁姝ワ細鍙戦€佽姹�  灏嗚姹傚弬鏁板啓鍦║RL涓�
        /**
         * 鑾峰彇鏁版嵁鍚庣殑澶勭悊绋嬪簭
         */
        httpRequest.onreadystatechange = function () {
            if (httpRequest.readyState == 4 && httpRequest.status == 200) {
                var json = httpRequest.responseText;//鑾峰彇鍒癹son瀛楃涓诧紝杩橀渶瑙ｆ瀽
                fn&&fn(json);
            }
        };
    }
    //鍒朵綔棰勮
    function buildPreview(info,c_w,fn){
        let _this = this
        let ctx
        let w = 0
        let h = 0
        let bg_img = new Image()
        bg_img.crossOrigin = '*';
        bg_img.onload = ()=>{
            w = bg_img.naturalWidth
            h = bg_img.naturalHeight
            if(c_w){
                var img_r = c_w/w
                w = c_w
                h = h*img_r
            }
            
            // #ifdef H5
            loadImgs(()=>{
                buildCanvas()
            })
            // #endif
        }
        bg_img.src = info.bg_img
        
        function buildCanvas(){
            let canvas = document.createElement('canvas');
            ctx = canvas.getContext('2d');
            let r = w/info.width
            canvas.width = w
            canvas.height = h
            ctx.drawImage(bg_img,0,0,w,h)
            for(var i in info.config){
                var _item = info.config[i]
                if(_item.type == 1){
                    ctx.fillStyle = _item.color
                    var _x = 0
                    if(_item.align == 1){
                        ctx.textAlign = 'left';
                        _x = _item.x*r
                    }
                    else if(_item.align == 2){
                        ctx.textAlign = 'center';
                        _x = (_item.x+(_item.width*0.5))*r
                    }
                    else if(_item.align == 3){
                        ctx.textAlign = 'right';
                        _x = (_item.x+_item.width)*r
                    }
                    ctx.font = (_item.size*r)+"px 榛戜綋"
                    ctx.fillText(_item.text, _x, ((parseInt(_item.y)+parseInt(_item.size))*r))
                }
                else if(_item.type == 2){
                    try{
                        ctx.drawImage(_item.img, _item.x*r, _item.y*r, _item.width*r, _item.height*r)
                    }
                    catch(e){}
                }
            }
            fn&&fn(canvas.toDataURL());
        }
        
        function loadImgs(fn){
            var img_num = 0
            for(var i in info.config){
                var _item = info.config[i]
                if(_item.type == 2){
                    img_num++
                    _item.img = new Image()
                    _item.img.crossOrigin = 'anonymous';
                    _item.img.onload = function(){
                        img_num--
                        if(img_num<=0)fn&&fn()
                    }
                    _item.img.onerror = function(){
                        img_num--
                        if(img_num<=0)fn&&fn()
                    }
                    _item.img.src = _item.url
                }
            }
            if(img_num<=0)fn&&fn()
        }
    }
})(window);